<template>
  <van-form class="bankInfo" @submit="applyBankAfter" ref="bank">
    <!-- 允许输入正整数，调起纯数字键盘 -->
    <van-field
      v-model="info.certification_legalrepresentative"
      label="收款人（法人）"
      placeholder="收款人（法人）"
      readonly
    />
    <van-field
      v-model="info.bankcardno"
      label="银行卡号"
      placeholder="银行卡号"
      :rules="[{ required: true, message: '请填写' }]"
      required
    />
    <van-field
      v-model="info.bankofdeposit"
      label="开户行"
      placeholder="××银行××支行"
      :rules="[{ required: true, message: '请填写' }]"
      required
    />
  </van-form>
</template>
<script>
import { reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { UpdateCertification, getMemberBarkInfo } from "@/api/profit";
import { Toast } from "vant";
export default {
  setup() {
    const data = reactive({
      info: {
        member_id: "", //会员id
        apply_id: "", //资质申请id
        certification_id: "", //资质申请明细ID
        certification_legalrepresentative: "", //法人代表（账户名称）
        bankcardno: "", //银行卡号
        bankofdeposit: "", //开户行
      },
      bank: "",
    });
    const store = useStore();
    const onLoad = async () => {
      //获取银行信息
      const result = await getMemberBarkInfo({
        member_id: store.state.userInfo.F_UserId,
      });
      if (!result[0]||result[1].length==0) {
        Toast("暂无用户信息，请确认已完成店主资格认证！");
        return Promise.reject();
      }
       data.info = result[1][0];
      return Promise.resolve();
    };
    const applyBank = async () => {
      try {
        await data.bank.validate();
        return applyBankAfter();
      } catch (err) {
        return Promise.reject();
      }
    };
    const applyBankAfter = async () => {
      const result = await UpdateCertification({data:data.info});

      if (!result[0]) {
        return Promise.reject(result);
      }
      return Promise.resolve(result);
    };
    return { ...toRefs(data), applyBank, applyBankAfter,onLoad };
  },
};
</script>
<style lang="scss" scoped>
.bankInfo {
  min-height: 30rem;
}
</style>